package hung.android.pro1;


import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class UserDBAdapter {
	public static final String KEY_ROWID="id";
	public static final String KEY_CURRENTMONEY="currentmoney";
	public static final String KEY_ADD="add_money";
	public static final String KEY_SUB="sub_money";
	public static final String KEY_TIME="datetime";
    public static final String KEY_POSITION="position";
	
	public static final String DATABASE_NAME="User_account";
	public static final String DATABASE_TABLE="usertable";
	public static final int DATABASE_VERSION=1;
	public static final String CREATE_TABLE=
	"CREATE TABLE usertable(id INTEGER PRIMARY KEY AUTOINCREMENT," +
	"username TEXT NOT NULL," +
	"password TEXT NOT NULL," +
	"fullname TEXT NOT NULL," +
	"adress TEXT NOT NULL," +
	"phone NUMBERIC NOT NULL," +
	"email TEXT NOT NUll);"; 
	private DBHelper ourHelper;
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;
//----------------------------CREATE SQLite DATABASE TABLE--------------------------------------
private static class DBHelper extends SQLiteOpenHelper{

		public DBHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
		}
        //---------------------------------
		@Override
		public void onCreate(SQLiteDatabase db) {
		//	db.execSQL("CREATE TABLE " + DATABASE_TABLE+ " ("+ KEY_ROWID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ KEY_NAME + " TEXT NOT NULL,"+ KEY_HOTNESS + " TEXT NOT NULL);
		db.execSQL(CREATE_TABLE);
		//---> Create a table named "DATABASE_TABLE". It has 3 columns(KEY_ROWID, KEY_NAME, KEY_HOT_NESS).
					
					}
        //---------------------------------
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS "+ DATABASE_TABLE );//---> Insert a row at the bottom of the DATABASE_TABLE table.
		    onCreate(db);
		}
		
	}
//-----------HotOrNot Constructor----------------------------------------------------------------------
	public UserDBAdapter(Context c){
		ourContext=c;
	}
//------------open method();------------------------
	public UserDBAdapter open() throws SQLException{
		ourHelper=new DBHelper(ourContext);
		ourDatabase= ourHelper.getWritableDatabase();
		return this;//---> save the setting to HotOrNot object. Ex: hotornot= new HotOrNot(Context c); hotornot.open();
		
	}
//---------------close ---------------------
	public void close(){
		ourHelper.close();
	}
//-----------------------------------
	public long add_row(String currentmoney, String last_add,String last_sub,String datetime,String position) {
		// TODO Auto-generated method stub
		ContentValues cv=new ContentValues();//--->cv is the same as an array.
		cv.put(KEY_CURRENTMONEY, currentmoney);
		cv.put(KEY_ADD, last_add);
		cv.put(KEY_SUB, last_sub);
		cv.put(KEY_TIME, datetime);
		cv.put(KEY_POSITION, position);
		return ourDatabase.insert(DATABASE_TABLE, null, cv);//---> put "name" and "hotness" to 2 columns in new row,
	}
//--------------------------------------------------------------
	public String get_allData() {
		// TODO Auto-generated method stub
		String[] columns =new String[]{KEY_ROWID,KEY_CURRENTMONEY,KEY_ADD,KEY_SUB,KEY_TIME,KEY_POSITION};//-->list of columns to be returned.
		Cursor c=ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
		String result="";
		int iRow=c.getColumnIndex(KEY_ROWID);
		int iCurrentmoney=c.getColumnIndex(KEY_CURRENTMONEY);
		int iAdd=c.getColumnIndex(KEY_ADD);
		int iSub=c.getColumnIndex(KEY_SUB);
		int iTime=c.getColumnIndex(KEY_TIME);
		int iPosition=c.getColumnIndex(KEY_POSITION);
		  for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
		  result=result+c.getString(iRow)+ "\n" + c.getString(iCurrentmoney)+ "\n" + c.getString(iAdd)+ "\n" + c.getString(iSub)+ "\n" + c.getString(iTime)+ "\n" + c.getString(iPosition) + "\n";
	  }
		c.close();//---> to destroy the Cursor.
		
		return result;
	}
//----------------Transfers all database to Array!--------------
	public ArrayList<String> get_arrayData() {
		// TODO Auto-generated method stub
		int i=0;
		ArrayList<String> array = new ArrayList<String>();
		String[] columns =new String[]{KEY_ROWID,KEY_CURRENTMONEY,KEY_ADD,KEY_SUB,KEY_TIME,KEY_POSITION};//-->list of columns to be returned.
		Cursor c=ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
		String result="";
		int iRow=c.getColumnIndex(KEY_ROWID);
		int iCurrentmoney=c.getColumnIndex(KEY_CURRENTMONEY);
		int iAdd=c.getColumnIndex(KEY_ADD);
		int iSub=c.getColumnIndex(KEY_SUB);
		int iTime=c.getColumnIndex(KEY_TIME);
		int iPosition=c.getColumnIndex(KEY_POSITION);
		  for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
          array.add(i, c.getString(iRow) +"."+ c.getString(iTime)+"."+ c.getString(iPosition));
	      i+=1;
	      }
		c.close();//---> to destroy the Cursor.
		
		return array;
	}
//-------------------Get an column dat to array
	public ArrayList<String> get_column_arrayData(String KEY) {
		// TODO Auto-generated method stub
		int i=0;
		ArrayList<String> array = new ArrayList<String>();
		String[] columns =new String[]{KEY_ROWID,KEY_CURRENTMONEY,KEY_ADD,KEY_SUB,KEY_TIME,KEY_POSITION};//-->list of columns to be returned.
		Cursor c=ourDatabase.query(DATABASE_TABLE,columns, null, null, null, null, null);
		for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
          array.add(i, c.getString(c.getColumnIndex(KEY)));
	      i+=1;
	      }
		c.close();//---> to destroy the Cursor.
		
		return array;
	}
	
	
//---------------Get a particular row info----------------
	public String get_rowId(long rowId) throws SQLException
	{ String[] column= new String[]{KEY_ROWID,KEY_CURRENTMONEY,KEY_ADD,KEY_SUB,KEY_TIME,KEY_POSITION};
		
		Cursor c=ourDatabase.query(true, DATABASE_TABLE, column, KEY_ROWID + "=" + rowId, null, null, null,null,null);
		if(c==null){c.moveToFirst();}
		//------------------
		String result="";
		int iRow=c.getColumnIndex(KEY_ROWID);
		int iCurrentmoney=c.getColumnIndex(KEY_CURRENTMONEY);
		int iAdd=c.getColumnIndex(KEY_ADD);
		int iSub=c.getColumnIndex(KEY_SUB);
		int iTime=c.getColumnIndex(KEY_TIME);
		int iPosition=c.getColumnIndex(KEY_POSITION);
		//----------------------
		result=result+c.getString(iRow)+ " " 
		      + c.getString(iCurrentmoney)+ " "
			  + c.getString(iAdd)+ " " 
		      + c.getString(iSub)+ " " 
			  + c.getString(iTime)+ " " 
		      + c.getString(iPosition) + "\n";
		//----------------------------
		c.close();//--->destroys the cursor.
		return result;
	}
//-----------Insert a row Id--------------------------------------------------------
	public boolean insert_rowId(long rowId, String currentmoney,String add,String sub,String time,String position)
		{   ContentValues args=new ContentValues();
		    args.put(KEY_CURRENTMONEY,currentmoney);
		    args.put(KEY_ADD, add);
		    args.put(KEY_SUB, sub);
		    args.put(KEY_TIME,time); 
		    args.put(KEY_POSITION, position);
		return ourDatabase.update(DATABASE_TABLE, args, KEY_ROWID+"="+ rowId,null)>0;
		}
//--------------Delete a row having row Id------------------------------------------
	public boolean delete_rowId(long rowId)
	{
		return ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null)>0;
	}
//--------------Delete a row having row Id-------------------------------------------
	public boolean delete_alldata()
		{
			return ourDatabase.delete(DATABASE_TABLE, null, null)>0;
		}
//-----------------Rearrange RowID--------------------------------
	public boolean rearrange_rowId()
	{
		return false;
		
	}
//----------------------------------------------------------------

}
